library(foreign)
electionDat <- read.dta("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/matchingReplication.dta")

quintile <- function(x,slices=16){
	quints <- quantile(x,prob=seq(0,1,length=slices+2),na.rm=T)[-c(1,slices+2)]
	qtile <- 1
	for(i in 1:length(quints)){
		qtile <- qtile + (x>quints[i])
	}
	qtile
}


###### ATE #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))



ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATE <- ATEwin



###### ATT #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))



ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATT <- ATTwin

###### ATC #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))



ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")

ATEvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")
ATTvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATCvs <- Match(dat$Cvs,Tr=dat$CNtriv,X=dat[,c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATC")



ATTwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","CNtriv","year","Rinc","Dinc","Ivs")],exact=T,estimand="ATT")
ATEwinQ <- Match(dat$Cwin,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

ATEvsQ <- Match(dat$Cvs,Tr=dat$Cqual,X=dat[,c("Cexp",  "Iexp","year","CNtriv","Rinc","Dinc","Ivs")],exact=T,estimand="ATE")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=as.character(electionDat$yrStDist)),stringsAsFactors=F)

balchk <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATC <- ATCwin

### Save Results ###

treated <- as.character(dat$yrStDist[ATE$index.treated])
control <- as.character(dat$yrStDist[ATE$index.control])

resultDat <- data.frame(treated,control,stringsAsFactors=F)

write.csv(resultDat,"/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/matchedObs.csv",row.names=F)




####### Create Balance Table ########

ATTtab <- matrix(0,4,3)
ATEtab <- matrix(0,4,3)
ATCtab <- matrix(0,4,3)

balATT <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATT,data=dat)
balATE <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATE,data=dat)
balATC <- MatchBalance(CNtriv~Cexp+Iexp+Ivs+Cqual+Rinc+Dinc,match.out=ATC,data=dat)

rownames(ATTtab) <- rownames(ATEtab) <- rownames(ATCtab) <- c("Mean (treated)","Mean (control)","KS test","t-test")

# Get post-matching mean treatment, mean controlled, t.test-p, kstest-p
for(i in 1:3){
	attres <- balATT[[2]][[i]]
	ateres <- balATE[[2]][[i]]
	atcres <- balATC[[2]][[i]]
	ATTtab[1,i] <- attres$mean.Tr
	ATEtab[1,i] <- ateres$mean.Tr
	ATCtab[1,i] <- atcres$mean.Tr
	ATTtab[2,i] <- attres$mean.Co
	ATEtab[2,i] <- ateres$mean.Co
	ATCtab[2,i] <- atcres$mean.Co
	ATTtab[3,i] <- attres$ks$ks.boot.pvalue
	ATEtab[3,i] <- ateres$ks$ks.boot.pvalue
	ATCtab[3,i] <- atcres$ks$ks.boot.pvalue
	ATTtab[4,i] <- attres$tt$p.value
	ATEtab[4,i] <- ateres$tt$p.value
	ATCtab[4,i] <- atcres$tt$p.value
}
library(xtable)
xtable(cbind(ATTtab,ATEtab,ATCtab,digit=3))

####### Barplot depicting estimates ########
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/","effects.pdf",sep="")
pdf(filet,width=6,heigh=4,family="Times",pointsize=16)
par(las=1,mar=c(2,4,1,1))
library(gplots)
bars <- c(ATT$est,ATE$est,ATC$est)
se <- c(ATT$se,ATE$se,ATC$se)
cil= bars-1.96*se
ciu= bars+1.96*se
barplot2(bars,width=.5,plot.ci=T,ci.l=cil,ci.u=ciu,plot.grid=T,ylab="Effect on Pr(win)",grid.col="grey50",grid.lwd=1.25,names.arg=c("ATT","ATE","ATC"),space=.7)

dev.off()





